﻿using System;
using System.Collections.Generic;
using EzDesk.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace EzDesk.DesktopClient.Storage
{
    public class CacheStorage : GenericDictionary<CacheStorage>
    {
        public CacheStorage()
        {
            _cache = new Dictionary<string, object>();
        }

        private readonly Dictionary<string, object> _cache;

        public void Clear()
        {
            _cache.Clear();
        }

        public void Set<T>(string  key,T value)
        {
            _cache[key] = value;
        }

        public void Set(string key,object value)
        {
            _cache[key] = value;
        }
        public object Get(string key)
        {
            if(_cache.ContainsKey(key))
                return _cache[key];
            return null;
        }

        public T Get<T>(string key)
        {
            if (_cache.ContainsKey(key))
                return (T)_cache[key];
            return default(T);
        }
    }

    public class DataStorage
    {
        protected readonly string RelatedPath = "user\\data";

        public string GetFilePath(string name, string storeType)
        {
            string folderName = Path.Combine(PathHelp.CurrentPath, RelatedPath, storeType);
            if (!Directory.Exists(folderName))
                Directory.CreateDirectory(folderName);
            return Path.Combine(folderName, name);
        }

        /// <summary>
        /// Save data in client side
        /// </summary>
        /// <param name="data">data</param>
        /// <param name="name">filename(common is current username)</param>
        /// <param name="storeType">the base folders</param>
        /// <returns></returns>
        public DataStorage Save(object data, string name, string storeType)
        {
            string filePath = this.GetFilePath(name, storeType);
            using (FileStream stream = File.Create(filePath))
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, data);
                stream.Close();
            }
            return this;
        }

        /// <summary>
        /// Load data from client side
        /// </summary>
        /// <typeparam name="T">data type</typeparam>
        /// <param name="name">filename(common is current username)</param>
        /// <param name="storeType">folders</param>
        /// <returns></returns>
        public T Load<T>(string name, string storeType)
        {
            string filePath = this.GetFilePath(name, storeType);

            if (File.Exists(filePath))
            {
                using (FileStream stream = File.Open(filePath, FileMode.Open))
                {
                    var formatter = new BinaryFormatter();
                    try
                    {
                        T data = (T)formatter.Deserialize(stream);
                        return data;
                    }
                    catch(Exception ex)
                    {
                        return default(T);
                    }
                    finally
                    {
                        stream.Close();
                    }
                }
            }
            else
                return default(T);
        }
    }
}
